import numpy as np
from h5py import File
from pathlib import Path
# HV imports
import holoviews as hv
from holoviews import opts
hv.extension('bokeh', 'matplotlib')
import xarray as xr
# Memory profiler - OPTIONAL for testing
# https://timothymonteath.com/articles/monitoring_memory_usage/
%load_ext memory_profiler
%memit
# Quick hack for slow internet connection!
%autosave 36000
# Import class - this requires pointing to the `tmoDataBase.py` file.
# See https://github.com/phockett/tmo-dev
# Import class from file
import sys
sys.path.append(r'/cds/home/p/phockett/dev/tmo-dev/tmo')
import tmoDataBase as tb
from vmi import VMI # VMI class
tb.setPlotDefaults(fSize=[500,500]) # Set plot defaults (optional)
# Set main data path
baseDir = Path('/reg/data/ana15/tmo/tmolw0618/scratch/preproc/v2')
# Setup class object and which runs to read.
data = VMI(fileBase = baseDir, runList = range(89,97+1), fileSchema='_preproc_elecv2')
# The class has a bunch of dictionaries holding info on the data
# data.runs['files']
# Read in the files with h5py
# There is very basic checking implemented currently, just to see if 'energies' is present.
data.readFiles()
np.histogram2d^ to construct images from two passed data dimensions, the default is the electron data ['yc','xc']. ^ Faster methods to be developed, this is currently a bit slow (10s of seconds).
signal and background filtersets. self.filters, and can be set manually, or with self.setFilters() (additive, or reset all). More filter options to come!
# Default filter settings
data.filters
# Add a filter to an existing filterset
data.setFilter({'signal':{'energies':[0.05, 0.1]}})
data.filters
# Add a filter to all filtersets
data.setFilter({'ts':3.185})
data.filters
# Add a new filterset
data.setFilter({'ref':{'gas':True, 'energies':0.05,'ts':3.185}})
data.filters
# For a full filter reset pass reset=True
# This is currently set to just ckear all filters
data.setFilter(reset=True)
data.filters
# Manual setting
data.filters = {'signal':{'gas':True,
'desc': 'Signal filter.'},
'bg':{'gas':False,
'desc': 'Background filter.'},
}
This defaults to all data and all filtersets, so may take a little while (~seconds to minutes).
# dims = ['yc','xc'] # Optionally pass dimensions to use from the raw data, the default is ['yc','xc']
data.genVMIXmulti()
This will generate images and output to an Xarray Dataset, self.imgStack, with dimensions (yc,xc,run) for each filterset.
data.imgStack
Filter parameters and image generation settings and metrics are set to self.data[run][filterset].
data.data[89]['signal']
# Subtraction, and set as new image set
data.imgStack['sub'] = data.imgStack['signal'] - data.imgStack['bg']
data.imgStack
(More to follow, but see the Xarray docs for general info.)
# The data can also be output as a 4D array, (x,y,run,name), which may be more useful for further computations
data.restackVMIdataset()
# To reduce network bandwidth requirements, for those with slow internet connections, the image stack can be downsampled prior to visualisation
data.downsample(step=[5,5])
# This is set to self.imgReduce, note this preserves coords.
data.imgReduce
# Single selected image by run and type
# Use the interactive histogram to adjust the colormapping, or pass clims = [min, max] to set explicitly
# Use the menu bar to pan, zoom, save and reset the image.
data.showImg(run=89, name='signal')
# For further use, the hv.Image object created can be returned and used...
# (See the HoloViews docs for more info: https://holoviews.org/getting_started/Introduction.html)
imgPlot = data.showImg(run=89, name='signal', returnImg=True)
# Plot image + 1D slices
# Note here the slices are selected by coods, and the plot axes are linked.
(imgPlot.hist() + imgPlot.sample(xc=500) + imgPlot.sample(yc=552)).cols(1)
# All images can be viewed with a HoloMap + widgets...
# # Firstly set to an hv.Dataset
# imgDS = hv.Dataset(data.restackVMIdataset())
# # Then a HoloMap of images
# imgDS.to(hv.Image, kdims=['xc','yc']).opts(colorbar=True).hist()
data.showImgSet()